package com.tiyende.credit.loop.ui.presenter

import com.tiyende.credit.loop.common.CommonPresenter
import com.tiyende.credit.loop.configs.TiyendeApiPath
import com.tiyende.credit.loop.configs.TiyendeApiUrl
import com.tiyende.credit.loop.configs.TiyendeApiUrl.addCommonParams
import com.tiyende.credit.loop.event.TiyendeEventPointHelper
import com.tiyende.credit.loop.event.TiyendeEventPointType
import com.tiyende.credit.loop.request.IHttpCallback
import com.tiyende.credit.loop.ui.contract.IViewContract
import com.tiyende.credit.loop.utils.shared.TiyendeSharedHelper
import com.tiyende.credit.loop.utils.shared.TiyendeSharedKey
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/27 13:45
 * @Annotation：校验 Presenter 实现
 */
class TiyendeCheckMobilePresenter(private val iView: IViewContract.ITiyendeCheckMobileView) : CommonPresenter(iView) {

    //选择类型  SMS、WHATSAPP、TELEGRAM、TGBOT
    private var selectType = ""
    //是否需要选择出生日期
    private var needSubmitBirthDay = "NO"

    fun getCheckNumberSelectType(): String{
        return selectType
    }

    /**
     * 是否需要验证生日
     */
    fun getCheckNumberNeedSubmitBirthDay(): String{
        return needSubmitBirthDay
    }

    /**
     * 切换校验类型
     */
    fun setCheckNumberType(type: String,isSend: Boolean = true){
        if (selectType == type){
            return
        }
        selectType = type
        iView.showTiyendeCheckMobileType(type,isSend)

        //如果校验类型是”TGBOT“，需要调接口获取Url去浏览器校验
        if ("TGBOT" == type){
            loadTiyendeCheckLinkAction()
            return
        }
        if (isSend){
            sendCheckNumberWhatsAppCodeAction()
        }
    }

    /**
     * 发送WhatsApp验证码
     */
    fun sendCheckNumberWhatsAppCodeAction(){
        //发送验证，埋点增加手机号
        TiyendeEventPointHelper.addEventPointRequestAction(TiyendeEventPointType.CLICK.TIYENDE_CLICK_VERIFICATION_GETCODE,iView.getTiyendeCheckMobile())
        iView.showTiyendeLoading()
        val loginMobile = TiyendeSharedHelper.queryTiyendeSharedString(TiyendeSharedKey.TIYENDE_MOBILE)
        val params = JSONObject().addCommonParams().apply {
            put("path", TiyendeApiPath.TIYENDE_PATH.TIYENDE_API_WHATSAPP_SEND_CODE_PATH)
            put("mobile", loginMobile)
            put("submitMobile", iView.getTiyendeCheckMobile())
            put("sendType",selectType)
        }
        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
        doHttpPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    //是否需要验证生日
                    if (obj.has("needSubmitBirthDay")){
                        needSubmitBirthDay = obj.optString("needSubmitBirthDay")
                    }
                    //当为YES时可以跳过，直接调用流程接口，根据流程步骤whatsappFlag标识走
                    val directLoginFlag = obj.optString("directLoginFlag")
                    iView.showTiyendeCheckMobileSendCodeSuccess(directLoginFlag)
                }
            }

            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hiddenTiyendeLoading()
            }
        })
    }

    /**
     * 当校验方式是”TGBOT“时，需要通过链接校验
     */
    fun loadTiyendeCheckLinkAction(){
        iView.showTiyendeLoading()
        val params = JSONObject().addCommonParams().apply {
            put("path", TiyendeApiPath.TIYENDE_PATH.TIYENDE_API_TELEGRAM_BOT_CHECK_URL_PATH)
            put("mobile", iView.getTiyendeCheckMobile())
        }
        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
        doHttpPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    val link = obj.optString("url","")
                    iView.showTiyendeCheckMobileTelegramBotLink(link)
                }
            }

            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hiddenTiyendeLoading()
            }
        })
    }

    /**
     * 校验验证码
     */
    fun onCheckNumberVerificationCodeAction(submitBirthDay: String = ""){
        //需要提交出生日期
        if (needSubmitBirthDay == "YES" && submitBirthDay.isEmpty()){
            iView.showTiyendeCheckMobileChooseBirthdayDialog()
            return
        }
        iView.showTiyendeLoading()
        val loginMobile = TiyendeSharedHelper.queryTiyendeSharedString(TiyendeSharedKey.TIYENDE_MOBILE)
        val params = JSONObject().addCommonParams().apply {
            put("path", TiyendeApiPath.TIYENDE_PATH.TIYENDE_API_WHATSAPP_CHECK_CODE_PATH)
            put("mobile", loginMobile)
            put("submitMobile", iView.getTiyendeCheckMobile())
            put("code",iView.getTiyendeCheckMobileCode())
            put("checkType",getCheckNumberSelectType())
            put("needSubmitBirthDay",needSubmitBirthDay)//是否需要验证生日
        }
        if (needSubmitBirthDay == "YES" && submitBirthDay.isNotEmpty()){
            params.put("submitBirthDay",submitBirthDay)//出生日期
        }
        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
        doHttpPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                iView.hiddenTiyendeLoading()
                iView.showTiyendeCheckMobileSuccess()
            }

            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hiddenTiyendeLoading()
            }
        })
    }
}